package com.pfs.internal.timestamp.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.DateUtil;

public class ExcelUtil {
	public static HSSFWorkbook excel2Workbook(File file) throws IOException {
		HSSFWorkbook wb = null;
		try {
			InputStream myxls = new FileInputStream(file.getPath());
			wb = new HSSFWorkbook(myxls);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
		return wb;
	}

	public static String getStringCellValue(HSSFRow row, int pos) {
		HSSFCell cell = row.getCell(pos);
		if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
			return null;
		}
		return cell.getStringCellValue();
	}

	public static double getNumericCellValue(HSSFRow row, int pos) {
		HSSFCell cell = row.getCell(pos);
		if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
			return 0;
		}
		return cell.getNumericCellValue();
	}

	public static Date getDateCellValue(HSSFRow row, int pos) {
		// HSSFCell cell = row.getCell(pos);
		// if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
		// return null;
		// }
		// return cell.getDateCellValue();
		HSSFCell currentCell = row.getCell(pos);
		
		try {
			String value = currentCell.getStringCellValue();
			//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			return Formater.str2date2(value);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
